<template>
  <div>
    <el-dialog
      :visible="isShowDialog"
      :destroy-on-close="isDestroyClose"
      :title="`${formData.id?'编辑':'添加'}房产`"
      @close="btnClose"
    >
      <el-form ref="formData" :model="formData" :rules="rules">
        <el-form-item label="经纪">
          <el-select v-model="formData.agent">
            <el-option
              v-for="item in agentsList"
              :key="item.id"
              :label="item.name"
              :value="item.id"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="分类">
          <el-select v-model="formData.category">
            <el-option
              v-for="item in categoryList"
              :key="item.id"
              :label="item.title"
              :value="item.id"
            />
          </el-select>
        </el-form-item>

        <el-form-item label="标题" prop="title">
          <el-input v-model="formData.title" size="small" style="width: 92%" />
        </el-form-item>
        <el-form-item label="标签" prop="tags">
          <el-tag
            v-for="tag in formData.tags"
            :key="tag"
            closable
            :disable-transitions="false"
            @close="handleClose(tag)"
          >
            {{ tag }}
          </el-tag>
          <el-input
            v-if="inputVisible"
            ref="saveTagInput"
            v-model="inputValue"
            class="input-new-tag"
            size="small"
            @keyup.enter.native="handleInputConfirm"
            @blur="handleInputConfirm"
          />
          <el-button v-else class="button-new-tag" size="small" @click="showInput">+ 新标签</el-button>
        </el-form-item>

        <el-form-item label="地址" prop="address">
          <el-dropdown>
            <el-button type="plain" size="small">
              全部分类<i class="el-icon-arrow-down el-icon--right" />
            </el-button>
            <el-dropdown-menu slot="dropdown">
              <el-dropdown-item>黄金糕</el-dropdown-item>
            </el-dropdown-menu>
          </el-dropdown>
          <el-input v-model="formData.address" size="small" style="width: 80%" />
        </el-form-item>

        <el-form-item label="价格" prop="price">
          <el-input v-model="formData.price" size="small" style="width: 94%" />
        </el-form-item>

        <el-form-item label="描述" prop="desc">
          <el-input v-model="formData.desc" size="small" style="width: 92%" type="textarea" rows="4" />
        </el-form-item>

        <el-form-item label="内容" />

        <el-form-item label="介绍图片" prop="photos">
          <img v-for="item in formData.photos" :key="item" class="infoImg" :src="item" alt="">
          <ImageUploads ref="formDataPhotos" />
        </el-form-item>

        <el-form-item label="封面" prop="image">
          <ImageUpload ref="formDataImage" />
        </el-form-item>
      </el-form>

      <el-row slot="footer" type="flex" justify="end">
        <el-col :span="6">
          <el-button type="primary" size="small" @click="btnOk">确定</el-button>
          <el-button size="small" @click="btnClose">取消</el-button>
        </el-col>
      </el-row>
    </el-dialog>
  </div>
</template>

<script>
import { addProperties, updateProperties, editProperties } from '@/api/properties'
import { getCategoryList } from '@/api/category'
import { getAgentsList } from '@/api/agents'
export default {
  props: {
    isShowDialog: {
      type: Boolean,
      default: false
    }
  },
  data() {
    return {
      formData: {
        address: '', // 地址
        agent: '', // 属于那个经纪的id值
        category: '', // 属于那个分类 id值
        desc: '', // 描述
        title: '', // 标题
        photos: [], // 介绍图片
        concent: '', // 内容
        price: '', // 价格
        image: '', // 封面
        tags: []// 标签
      },
      rules: {
        title: [
          { required: true, message: '标题不能为空', trigger: 'blur' }
        ],
        desc: [
          { required: true, message: '描述不能为空', trigger: 'blur' }
        ]
      },
      categoryList: [], // 所有分类列表
      agentsList: [], // 所有的经纪列表
      imgList: [],
      // 标签
      dynamicTags: [],
      inputVisible: false,
      isDestroyClose: false,
      inputValue: '',
      // 地址下拉菜单
      select: ''
    }
  },
  async created() {
    this.getCategoryList()
    this.getAgentsList()
  },
  methods: {
    async btnOk() {
      // const photos = this.$refs.formDataPhotos.fileList
      await this.$refs.formData.validate()
      const photos = this.$refs.formDataPhotos.fileList
      const imgUrl = photos.map(item => {
        return item.url
      })
      // console.log(imgUrl)
      this.formData.photos = imgUrl
      const image = this.$refs.formDataImage.fileList
      if (this.formData.id) {
        await updateProperties({ ...this.formData, image: image[0]?.url })
        this.$message.success('编辑成功')
      } else {
        await addProperties({ ...this.formData, image: image[0]?.url })
        this.$message.success('添加成功')
      }

      this.$emit('update:isShowDialog', false)
      this.$emit('addProperties')
    },
    async btnClose() {
      await this.$refs.formData.resetFields()
      this.isDestroyClose = true
      this.$emit('update:isShowDialog', false)
    },
    async editProperties(id) {
      this.formData = await editProperties(id)
      // console.log(this.formData)
      if (this.formData.image) {
        this.$refs.formDataImage.fileList = [
          { url: this.formData.image }
        ]
      }
      if (this.formData.photos) {
        this.$refs.formDataPhotos.fileList = [
          { url: this.formData.photos }
        ]
      }
    },

    // 标签方法
    handleClose(tag) {
      this.formData.tags.splice(this.formData.tags.indexOf(tag), 1)
    },

    showInput() {
      this.inputVisible = true
      this.$nextTick(() => {
        this.$refs.saveTagInput.$refs.input.focus()
      })
    },

    handleInputConfirm() {
      const inputValue = this.inputValue
      if (inputValue) {
        this.formData.tags.push(inputValue)
      }
      this.inputVisible = false
      this.inputValue = ''
    },
    async getCategoryList() {
      this.categoryList = await getCategoryList()
    },
    async getAgentsList() {
      this.agentsList = await getAgentsList()
    }
  }
}
</script>

<style scoped>
.infoImg {
  width: 150px;
  border-radius: 4px;
}
.el-tag + .el-tag {
    margin-left: 10px;
  }
  .button-new-tag {
    margin-left: 10px;
    height: 32px;
    line-height: 30px;
    padding-top: 0;
    padding-bottom: 0;
  }
  .input-new-tag {
    width: 90px;
    margin-left: 10px;
    vertical-align: bottom;
  }
  .el-select .el-input {
    width: 130px;
  }
  .input-with-select .el-input-group__prepend {
    background-color: #fff;
  }
</style>
